import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/app/my/invoicing/chooseHeader/bean/FP_PresetInvoice_Bean.dart';
import 'package:flutter_ahj_app/app/my/invoicing/chooseHeader/view/itemView.dart';
import 'package:flutter_ahj_app/app/my/invoicing/chooseHeader/viewModel/chooseHeaderViewModel.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JText.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/utils/screen_util.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';

class ChooseHeaderView extends BaseStatefulWidget {
  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    return _ChooseHeaderViewSate();
  }
}

class _ChooseHeaderViewSate
    extends BaseState<ChooseHeaderViewModel, ChooseHeaderView> {
  List<Data> _dataList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(Duration.zero, () {
      _getData();
    });
  }

  @override
  Widget initWidget() {
    // TODO: implement initWidget
    return Scaffold(
      backgroundColor: SQColor.lightGray,
      appBar: MTopBar(
          title: '抬头选择',
          rightButtonText: '添加',
          onRightButtonClick: () => _addEve()).create(),
      body: _dataList.isNotEmpty ? _bodyView() : _noDataView(),
    );
  }

  _bodyView() {
    return SafeArea(
      child: Container(
        child: ListView.builder(
          itemCount: _dataList.length,
          itemBuilder: (BuildContext context, int index) {
            return ItemView(
              voidCallback: () => _itemEve(index),
              data: _dataList[index],
            );
          },
        ),
      ),
    );
  }

  /// 没有数据
  _noDataView() {
    return Container(
      width: ScreenUtil.getScreenW(context),
      child: Column(
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(top: 100),
            child: Image.asset(
              'img/no_fp.png',
              height: 150,
              width: 167,
            ),
          ),
          Container(
            padding: EdgeInsets.only(top: 20),
            child: JText('暂无预设开票信息', color: SQColor.black_666666, fontSize: 14),
          ),
        ],
      ),
    );
  }

  ///添加
  _addEve() {
    Navigator.pushNamed(context, "/FPHeaderView").then((value) {
      if (value == '请求成功') {
        _getData();
      }
    });
  }

  ///获取数据
  _getData() async {
    viewModel.getData();
    viewModel.dataStream().listen((event) {
      if (event.code == 0 && event.data != null && event.data.length > 0) {
        setState(() {
          _dataList = event.data;
        });
      }
    });
  }

  ///item event
  _itemEve(int index) {/// 选择 开票 抬头
  Data data = _dataList[index];

  Navigator.pop<Data>(context,data);

  }
}
